package zhangtao.bwie.com.yuekao2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import Bean.RegistBean;
import Presenter.IPresenter;
import Views.IView;

/**
 * Created by ZhangTAO on 2017/12/21.
 */

public class Main2_act extends AppCompatActivity implements IView,View.OnClickListener{
    private IPresenter iPresenter;
    private HashMap<String, String> map;
    private Button regist_btn;
    private EditText regist_zhang;
    private EditText regist_pass;
    private String zhang;
    private String pass;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        initView();
        setOnClick();
    }

    private void initView() {
        regist_btn = (Button) findViewById(R.id.regist_btn);
        regist_zhang = (EditText) findViewById(R.id.regist_zhang);
        regist_pass = (EditText) findViewById(R.id.regist_pass);
    }

    private void setOnClick() {
        regist_btn.setOnClickListener(this);
    }

    public void setPresenter(String url, Map<String,String> map) {
        iPresenter = new IPresenter();
        iPresenter.attach(this);
        iPresenter.getData(url,map);
    }
    /**
     * 验证手机格式
     */
    public static boolean isMobile(String number) {
        String num = "[1][34578]\\d{9}";
        if (TextUtils.isEmpty(number)) {
            return false;
        } else {
            return number.matches(num);
        }
    }
    public boolean setUser(String zhang,String pass) {
        if(!isMobile(zhang)) {
            Toast.makeText(this,"手机号不合法",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(zhang == "" || pass == "") {
            Toast.makeText(this,"账号或密码不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(pass.length() < 6) {
            return false;
        }
        return true;
    }
    @Override
    public void onSuccess(Object o) {
        if(o instanceof RegistBean) {
            RegistBean bean = (RegistBean) o;
            if(bean.getCode().equals("0")) {
                Toast.makeText(this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }
    @Override
    public void onFailed(Exception e) {
        Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.regist_btn:
                zhang = regist_zhang.getText().toString().trim();
                pass = regist_pass.getText().toString().trim();
                boolean b = setUser(zhang, pass);
//                if(b) {
                    map = new HashMap<>();
                    map.put("mobile", zhang);
                    map.put("password ", pass);
                    setPresenter("http://120.27.23.105/",map);
//                }
                break;
        }
    }
}
